<?php
/*------------------------------------------------------------------------
# Advanced blogs Extension - Module for OpenCart 1.5.1.x
# ------------------------------------------------------------------------
# Copyright (C) 2011 OpenCartSoft.com. All Rights Reserved.
# @license - Copyrighted Commercial Software
# Author: www.OpenCartSoft.com
# Websites:  http://www.opencartsoft.com -  Email: admin@opencartsoft.com
# This file may not be redistributed in whole or significant part.
-------------------------------------------------------------------------*/

class ControllerblogsAll extends Controller {
	public function index() {
		$this->language->load('blogs/all');
		$this->load->model('catalog/blogs');		
		$this->load->model('tool/image'); 
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else { 
			$page = 1;
		}	
		if (isset($this->request->get['limit'])) {
			$limit = $this->request->get['limit'];
		} else {
			$limit = $this->config->get('config_catalog_limit');
		}
		$data["see_more"] = $this->language->get("see_more");		
		$data['breadcrumbs'] = array();

   		$data['breadcrumbs'][] = array(
       		'text'      => $this->language->get('text_home'),
			'href'      => $this->url->link('common/home'),
       		'separator' => false
   		);
   		$query_blogs = array(
   				'sort'  => 'n.date_added',
				'order' => 'DESC',
				'start'              => ($page - 1) * $limit,
				'limit'              => $limit
			);
		
		
		$blogs_total = $this->model_catalog_blogs->getTotalblogs($query_blogs); 
		if ($blogs_total > 0) {
			
	  		$this->document->setTitle($this->language->get('text_blogs'));			
			
			$data['heading_title'] = $this->language->get('text_blogs');
			
			$data['text_blogs'] = $this->language->get('text_blogs');
			$data['text_empty'] = $this->language->get('text_empty');			
			
			$data['text_display'] = $this->language->get('text_display');
			$data['text_limit'] = $this->language->get('text_limit');					
			$data['text_post_on'] = $this->language->get('text_post_on');
			$data['text_viewed'] = $this->language->get('text_viewed');
			
			$data['button_continue'] = $this->language->get('button_continue');
			
			$data['breadcrumbs'][] = array(
  			'text'      => $this->language->get('text_blogs'),
			'href'      => $this->url->link('blogs/all'),
  			'separator' => $this->language->get('text_separator')
  		);
			
		$url = '';
			
		if (isset($this->request->get['limit'])) {
			$url .= '&limit=' . $this->request->get['limit'];
		}		
			
			$data['blogs'] = array();			
		
			$results = $this->model_catalog_blogs->getblogs($query_blogs);
	
			
			foreach ($results as $result) {
				$width = ''; $height = '';
				if ($result['image']) {
					$image = $this->model_tool_image->resize($result['image'],640,480);
				} else {
					$firstImgblogs = $this->catchFirstImage(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8'));
					if($firstImgblogs == 'no_image.jpg'){
						$image = $this->model_tool_image->resize('no_image.jpg',640,480);
					}else{
						$image = $firstImgblogs; $width = 'width="'."282".'"'; $height = 'height="148"';
					}					
				}
				
				if (empty($result['short_description'])) {
					$short_description = substr(strip_tags(html_entity_decode($result['description'], ENT_QUOTES, 'UTF-8')), 0, 500).' ...';
				}else{
					$short_description = $result['short_description'];
				}
				$data['blogs'][] = array(
					'blogs_id'  => $result['blogs_id'],
					'thumb'       => $image,
					'name'        => $result['name'],
					'short_description'       => $short_description,
					'date_added'       => date('H:i:s - '.$this->language->get('date_format_short'), strtotime($result['date_added'])),					
					'width'      => $width, 
					'height'      => $height, 	
					'viewed'      => $result['viewed'], 
					'href'        => $this->url->link('blogs/blogs', '&blogs_id=' . $result['blogs_id'])
				);
			}
			
			$url = '';
	
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
			
			$url = '';		
			
			$data['limits'] = array();
			
			$data['limits'][] = array(
				'text'  => $this->config->get('config_catalog_limit'),
				'value' => $this->config->get('config_catalog_limit'),
				'href'  => $this->url->link('blogs/all', $url . '&limit=' . $this->config->get('config_catalog_limit'))
			);
						
			$data['limits'][] = array(
				'text'  => 25,
				'value' => 25,
				'href'  => $this->url->link('blogs/all', $url . '&limit=25')
			);
			
			$data['limits'][] = array(
				'text'  => 50,
				'value' => 50,
				'href'  => $this->url->link('blogs/all', $url . '&limit=50')
			);

			$data['limits'][] = array(
				'text'  => 75,
				'value' => 75,
				'href'  => $this->url->link('blogs/all', $url . '&limit=75')
			);
			
			$data['limits'][] = array(
				'text'  => 100,
				'value' => 100,
				'href'  => $this->url->link('blogs/all', $url . '&limit=100')
			);
						
			$url = '';			
	
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
			$pagination = new Pagination();
			$pagination->total = $blogs_total;
			$pagination->page = $page;
			$pagination->limit = $limit;
			$pagination->text = $this->language->get('text_pagination');
			$pagination->url = $this->url->link('blogs/all', $url . '&page={page}');
		
			$data['pagination'] = $pagination->render();		
			
			$data['limit'] = $limit;
		
			$data['continue'] = $this->url->link('common/home');
			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/blogs/all.tpl')) {
				$template = $this->config->get('config_template') . '/template/blogs/all.tpl';
			} else {
				$template = 'default/template/blogs/all.tpl';
			}

				$data["column_left"] = $this->load->controller('common/column_left');
				$data["column_right"] = $this->load->controller('common/column_right');
				$data["content_top"] = $this->load->controller('common/content_top');
				$data["content_bottom"] = $this->load->controller('common/content_bottom');
				$data["footer"] = $this->load->controller('common/footer');
				$data["header"] = $this->load->controller('common/header');
				
				$data["blogs_category"] = $this->load->controller('module/blogs_category');	
				$this->response->setOutput($this->load->view($template, $data));
							
													
    		} else {
				
			$url = '';
			if (isset($this->request->get['cat_id'])) {
				$url .= '&cat_id=' . $this->request->get['cat_id'];
			}			
				
			if (isset($this->request->get['page'])) {
				$url .= '&page=' . $this->request->get['page'];
			}
						
			if (isset($this->request->get['limit'])) {
				$url .= '&limit=' . $this->request->get['limit'];
			}
			$data['breadcrumbs'][] = array(
				'text'      => $this->language->get('text_error'),
				'href'      => $this->url->link('blogs/all', $url),
				'separator' => $this->language->get('text_separator')
			);
				
			$this->document->setTitle($this->language->get('text_error'));

      		$data['heading_title'] = $this->language->get('text_error');

      		$data['text_error'] = $this->language->get('text_error');

      		$data['button_continue'] = $this->language->get('button_continue');

      		$data['continue'] = $this->url->link('common/home');

			if (file_exists(DIR_TEMPLATE . $this->config->get('config_template') . '/template/error/not_found.tpl')) {
				$template = $this->config->get('config_template') . '/template/error/not_found.tpl';
			} else {
				$template = 'default/template/error/not_found.tpl';
			}
			
			$data["column_left"] = $this->load->controller('common/column_left');
			$data["column_right"] = $this->load->controller('common/column_right');
			$data["content_top"] = $this->load->controller('common/content_top');
			$data["content_bottom"] = $this->load->controller('common/content_bottom');
			$data["footer"] = $this->load->controller('common/footer');
			$data["header"] = $this->load->controller('common/header');
			$data["blogs_category"] = $this->load->controller('module/blogs_category');	
			$this->response->setOutput($this->load->view($template, $data));
		}
  }
  
  function catchFirstImage($content) {
	  $first_img = ''; 
	  $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
	  if(isset($matches[1][0])){
	  	$first_img = $matches[1][0];
		}
	  if(empty($first_img)){ //Defines a default image
	    $first_img = "no_image.jpg";
	  }
	  return $first_img;
	} //end function
}
?>